﻿using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Infrastructure;
using System;
using System.Web.Mvc;

namespace Logistics.Web.Infrastructure
{
    public class MobileRedirectAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext == null || filterContext.HttpContext == null)
                return;

            if (EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.IsAdmin())
            {
                return;
            }

            var mobileDeviceHelper = EngineContext.Current.Resolve<Nop.Services.Common.IMobileDeviceHelper>();

            if (!mobileDeviceHelper.IsMobileDevice(filterContext.HttpContext))
            {
                return;
            }

            var mobileUrl = mobileDeviceHelper.RedirectUrlIfMobileDevice(filterContext.HttpContext);
            if (!String.IsNullOrEmpty(mobileUrl))
            {
                filterContext.Result = new RedirectResult(mobileUrl);
            }
        }
    }
}